package com.sls.web.manager.game.service.impl;

import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.game.pojo.GoodsSupply;
import com.sls.web.manager.game.querybean.GoodsSupplyQuery;
import com.sls.web.manager.game.service.GoodsSupplyService;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.socket.SuperService;

@Repository("goodsSupplyService")
public class GoodsSupplyServiceImpl extends SuperService implements
		GoodsSupplyService {
	private Gson gson;

	public GoodsSupplyServiceImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public boolean add(GoodsSupply goodsSupply, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.GOODS_SUPPLY_ADD);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		goodsSupply.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, goodsSupply);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode())) {
			return true;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public boolean update(GoodsSupply goodsSupply, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.GOODS_SUPPLY_UPDATE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, goodsSupply);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode())) {
			return true;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public Pagination<GoodsSupply> queryGoodsSupply(
			GoodsSupplyQuery goodsSupplyQuery, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.GOODS_SUPPLY_PAGE_QUERY);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, goodsSupplyQuery);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			Pagination<GoodsSupply> goodsSupply = gson.fromJson(body,
					new TypeToken<Pagination<GoodsSupply>>() {
					}.getType());
			return goodsSupply;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public GoodsSupply queryGoodsSupplyById(String id, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.GOODS_SUPPLY_QUERY_BY_ID);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		GoodsSupply goodsSupply = new GoodsSupply();
		goodsSupply.setId(id);
		SocketDTO dto = super.send(head, goodsSupply);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode())) {
			GoodsSupply goodsSupply2 = gson.fromJson(dto.getBody(),
					GoodsSupply.class);
			return goodsSupply2;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public boolean updateState(String id, String state, String goodsId,
			UserInfoVO userInfoVO) throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.GOODS_SUPPLY_UPDATE_STATE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		GoodsSupply goodsSupply = new GoodsSupply();
		goodsSupply.setId(id);
		goodsSupply.setGoodsId(goodsId);
		goodsSupply.setGoodsState(state);
		goodsSupply.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, goodsSupply);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode())) {
			return true;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public List<String> querySupplyNames(UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("020938");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			if (StringUtils.isNotEmpty(body)) {
				List<String> nameList = gson.fromJson(body,
						new TypeToken<List<String>>() {
						}.getType());
				return nameList;
			}
			return null;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public List<GoodsSupply> querySupplyInfo(String supplierName,
			String status, UserInfoVO userInfoVO) throws Exception {
		GoodsSupplyQuery goodsSupplyQuery = new GoodsSupplyQuery();
		goodsSupplyQuery.setCurrentPage(1);
		goodsSupplyQuery.setPageSize(20);
		goodsSupplyQuery.setSupplyName(supplierName);
		goodsSupplyQuery.setGoodsState(status);

		List<GoodsSupply> supplyList = new LinkedList<GoodsSupply>();
		Pagination<GoodsSupply> page = queryGoodsSupply(goodsSupplyQuery,
				userInfoVO);
		if (page != null) {
			if (page.getList() != null) {
				supplyList.addAll(page.getList());
			}
			int count = page.getRowCount();
			if (goodsSupplyQuery.getPageSize() == 0) {
				goodsSupplyQuery.setPageSize(1);
			}
			for (int i = 1; i < Math.ceil(count * 1.0
					/ goodsSupplyQuery.getPageSize()); i++) {
				goodsSupplyQuery.setCurrentPage(i + 1);
				page = queryGoodsSupply(goodsSupplyQuery, userInfoVO);
				if (page.getList() != null) {
					supplyList.addAll(page.getList());
				}
			}
			return supplyList;
		}
		return null;
	}

}
